﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

using XEvents.Library;
using System.Configuration;
using System.Collections.Specialized;
using ExtendedEventsManager.Properties;

namespace ExtendedEventsManager
{
    public partial class ConnectionInfoForm : Form
    {
        private SqlServer server;
        Settings s = Properties.Settings.Default;

        public SqlServer ServerInfo
        {
            get { return server; }
        }

        public ConnectionInfoForm()
        {
            InitializeComponent();
            if (s.ServerAutoComplete == null)
                s.ServerAutoComplete = new StringCollection();
            cmbx_Servers.AutoCompleteCustomSource = GetAutoComplete();
            cmbx_Servers.DataSource = s.ServerAutoComplete;
            checkBox1.Checked = s.IntegratedAuthentication;
        }

        private AutoCompleteStringCollection GetAutoComplete()
        {
            AutoCompleteStringCollection acsc = new AutoCompleteStringCollection();
            foreach (string st in s.ServerAutoComplete)
                acsc.Add(st);
            return acsc;
        }


        private void btn_Connect_Click(object sender, EventArgs e)
        {
            //backgroundWorker1.RunWorkerAsync();
            Login();
        }

        public SqlServer ValidateLogin()
        {
                if (!checkBox1.Checked)
                    return new SqlServer(cmbx_Servers.Text, textBox2.Text, textBox3.Text);
                else
                    return new SqlServer(cmbx_Servers.Text);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            textBox3.Enabled = !checkBox1.Checked;
            textBox2.Enabled = !checkBox1.Checked;
        }

        private void ConnectionInfo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                backgroundWorker1.RunWorkerAsync();
            }
            if (e.KeyChar == (char)Keys.Escape)
            {
                this.HandleEscape();
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.HandleEscape();        
        }

        private void HandleEscape()
        {
            if (backgroundWorker1.IsBusy)
                backgroundWorker1.CancelAsync();
            else
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }

        private void Login()
        {
            try
            {
                SqlServer srv = ValidateLogin();
                //s.ServerAutoComplete = (StringCollection)cmbx_Servers.DataSource;
                if (s.KeepRecent)
                {
                    if (s.ServerAutoComplete.Contains(cmbx_Servers.Text))
                        s.ServerAutoComplete.Remove(cmbx_Servers.Text);

                    s.ServerAutoComplete.Insert(0, cmbx_Servers.Text);

                    while (s.ServerAutoComplete.Count > s.MaxRecent)
                    {
                        s.ServerAutoComplete.RemoveAt(s.MaxRecent);
                    }
                }
                s.IntegratedAuthentication = checkBox1.Checked;

                s.Save();
                
                this.server = srv;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                SqlServer srv = ValidateLogin();
                e.Result = srv;
            }
            catch (Exception ex)
            {
                e.Result = ex;
            }

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Result is Exception)
            {
                Exception ex = (Exception)e.Result;
                MessageBox.Show(ex.Message);
            }
            else if (e.Result is SqlServer)
            {
                this.server = (SqlServer)e.Result;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }

        }
    }
}
